package com.mc.moring.main.personal

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.ImageButton
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.mc.moring.R
import com.mc.moring.app.LogUtils
import com.mc.moring.base.BaseActivity
import com.mc.moring.base.HttpCallback
import com.mc.moring.bean.BankAccountListBean
import com.mc.moring.bean.ResultBean
import com.mc.moring.main.AddBandActivity
import com.mc.moring.main.modle.LoanModle
import com.mc.moring.main.modle.PermisosModle
import com.mc.moring.main.personal.adapter.BankAccountAdapter
import com.mc.moring.util.GsonUtils
import com.mc.moring.util.GsonUtils.getGson
import com.mc.moring.util.PointMgr
import com.mc.moring.util.ToastUtils
//import kotlinx.android.synthetic.main.actionbar_custom_shipper.abTitleTV


/**
 */
open class BankAccountListActivity : BaseActivity() {

    private var mListData = ArrayList<BankAccountListBean.DataBean.AccountsBean>()
    private val btn_back by lazy { findViewById<ImageButton>(R.id.abBackIBtn) }
    private val ll_add by lazy { findViewById<LinearLayout>(R.id.ll_add) }
    private val abTitleTV by lazy { findViewById<TextView>(R.id.abTitleTV) }

    lateinit var recyclerview: RecyclerView
    var adapter: BankAccountAdapter? = null

    private val REQUEST_CODE = 1

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        LogUtils.logE("my account page")
        setContentView(R.layout.activity_bank_account)
        init()
    }

    override fun getActivity(): Activity {
        return this
    }

    override fun initView() {
        abTitleTV.text = "Mi cuenta bancaria"
        btn_back.setOnClickListener {
            finish()
        }
        recyclerview = findViewById(R.id.recyclerview)
        val manager = LinearLayoutManager(this)
        recyclerview.layoutManager = manager
        adapter = BankAccountAdapter(mListData)
        recyclerview.adapter = adapter
        adapter?.setBankAccountListener{ accountId ->
            val map: MutableMap<String, String> = HashMap()
            map["bankAccountNo"] = accountId
            showLoading()
            PermisosModle.instant.deleteBankAccount(map, object : HttpCallback {
                override fun onSuccess(data: String) {
                    LogUtils.logE("delete bank account>$data")
                    val resultBean = getGson().fromJson(data, ResultBean::class.java)
                    runOnUiThread {
                        if (resultBean.code == "0000") {
                            initData()
                        } else {
                            ToastUtils.showShort(activity, resultBean.msg)
                        }
                    }
                    hideLoading()
                }

                override fun onFaill(e: String) {
                    LogUtils.logE("request fail>$e")
                    hideLoading()
                }
            })
        }
        ll_add.setOnClickListener {
            PointMgr.sendEvent("cl_person_infoof-24_open")
            Log.e("PointMgr.INSTANCE.sendEvent", "cl_person_infoof-24_open")
            val intent = Intent(this, AddBandActivity::class.java)
//            startActivity(intent)
            startActivityForResult(intent, REQUEST_CODE)
        }
    }

    override fun initData() {
        bankgetBankAccountList()
    }

    private fun bankgetBankAccountList() {
        val params: MutableMap<String, String> = java.util.HashMap()
        showLoading()
        LoanModle.instant.bankgetBankAccountList(params, object : HttpCallback {
            override fun onSuccess(data: String) {
                try {
                    var bankAccountListBean: BankAccountListBean? =
                        GsonUtils.getGson().fromJson(data, BankAccountListBean::class.java)
                            ?: return;
                    LogUtils.logE("account>" + GsonUtils.getGson().toJson(bankAccountListBean?.data?.accounts))
                    runOnUiThread {
                        bankAccountListBean?.data?.accounts?.let { accountList ->
                            mListData.clear()
                            accountList.forEach { account ->
                                mListData.add(account)
                            }
                            if (accountList.size >= 3) {
                                ll_add.visibility = View.GONE
                            } else {
                                ll_add.visibility = View.VISIBLE
                            }
                            adapter?.notifyDataSetChanged()
                        }
                    }
                } catch (e: Exception) {
                    e.printStackTrace()
                }
                hideLoading()
            }

            override fun onFaill(e: String) {
                hideLoading()
            }
        })
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        if (requestCode === REQUEST_CODE && resultCode === RESULT_OK) {
            val shouldRefresh: Boolean = data?.getBooleanExtra("refresh", false) == true
            if (shouldRefresh) {
                // 执行刷新操作
                initData()
            }
        }
    }

    public override fun onStop() {
        super.onStop()
        hideLoading()
    }
}
